home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / wattcp / src / pcconfig.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  5.6 KB  |  228 lines

  1. #include"capalloc.h"
  2. #include"capstdio.h"
  3. #include <copyright.h>
  4. #include <wattcp.h>
  5.  
  6. #include <io.h>
  7. #include <dos.h>    /* for _argv */
  8. #include <fcntl.h>    /* open modes */
  9. #include <ctype.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. #define MY_IP        "MY_IP"
  14. #define IP              "IP"
  15. #define NETMASK     "NETMASK"
  16. #define COOKIE        "COOKIE"
  17. #define NAMESERVER    "NAMESERVER"
  18. #define GATEWAY     "GATEWAY"
  19. #define DOMAINS        "DOMAINSLIST"
  20. #define HOSTNAME    "HOSTNAME"
  21. #define SOCKDELAY       "SOCKDELAY"
  22. #define ETHIP        "ETHIP"
  23. #define MSS             "MSS"
  24. #define BOOTP        "BOOTP"
  25. #define BOOTPTO        "BOOTPTO"
  26. #define DOMTO           "DOMAINTO"
  27. #define PRINT        "PRINT"
  28. #define INACTIVE        "INACTIVE"
  29. #define INCLUDE         "INCLUDE"
  30. #define MULTIHOMES      "MULTIHOMES"
  31.  
  32.  
  33. #define is_it( x ) if (!strcmp(name,x))
  34.  
  35. /*
  36.  * _inet_atoeth - read src, dump to ethernet buffer
  37.  *          and return pointer to end of text string
  38.  */
  39. char *_inet_atoeth( char *src, byte *eth )
  40. {
  41.     word count, val;
  42.     byte ch, icount;
  43.  
  44.     val = count = icount = 0;
  45.     while ((ch = toupper(*src++)) != 0 ) {
  46.         if (ch == ':' ) continue;
  47.         if (ch == ',' ) break;
  48.  
  49.         if ((ch -= '0') > 9) ch -= 7;
  50.         val = (val << 4) + ch;
  51.  
  52.         if ( icount ) {
  53.             eth[ count++ ] = val;
  54.             if (count > 5) break;
  55.             val = icount = 0;
  56.         } else
  57.             icount = 1;
  58.     }
  59.     if ( *src == ',' ) ++src;
  60.     return( src );
  61. }
  62.  
  63. static void ethip( char *s )
  64. {
  65.     eth_address temp_eth;
  66.     char *temp;
  67.  
  68.     if (( temp = _inet_atoeth( s, &temp_eth )) != NULL ) {
  69.     if (!memcmp( &temp_eth, &_eth_addr, sizeof( eth_address ))) {
  70.             my_ip_addr = inet_addr( temp );
  71.         }
  72.     }
  73. }
  74.  
  75. void _add_server( int *counter, int max, longword *array, longword value )
  76. {
  77.     int i,duplicate=0;
  78.  
  79.     if ( value && ( *counter < max )) {
  80.         for (i=0;i<*counter; i++) {
  81.             if ( array[i] == value)
  82.                 duplicate=1;
  83.         }
  84.         if (!duplicate)
  85.              array[ (*counter)++ ] = value;
  86.     }
  87. }
  88.  
  89. word sock_delay = 30;
  90. word sock_inactive = 0;  /* defaults to forever */
  91. word multihomes = 0;
  92. char defaultdomain[ 80 ];
  93. longword _cookie[ MAX_COOKIES ];
  94. int _last_cookie;
  95. extern _domaintimeout;
  96. void (*usr_init)() = NULL;
  97.  
  98. static set_values(char *name, char *value )
  99. {
  100.     char *p;
  101.     longword temp;
  102.     word i;
  103.  
  104.     strupr(name);
  105.     is_it( MY_IP ) {
  106.     if ( toupper( *value ) == 'B') _bootpon = 1;
  107.     else my_ip_addr = resolve( value );
  108.     }
  109.     is_it( IP ) {
  110.     if ( toupper( *value ) == 'B') _bootpon = 1;
  111.     else my_ip_addr = resolve( value );
  112.     }
  113.     else is_it( NETMASK) sin_mask = resolve( value );
  114.     else is_it( GATEWAY)
  115.     /* accept gateip[,subnet[,mask]]  */
  116.     _arp_add_gateway( value , 0L );
  117.     else is_it( NAMESERVER )  _add_server( &_last_nameserver,
  118.         MAX_NAMESERVERS, def_nameservers, resolve(value));
  119.     else is_it( COOKIE ) _add_server( &_last_cookie, MAX_COOKIES,
  120.         _cookie, resolve( value ));
  121.     else is_it( DOMAINS ) def_domain = strcpy( defaultdomain, value );
  122.     else is_it( HOSTNAME ) strncpy(_hostname, value, MAX_STRING );
  123.     else is_it( SOCKDELAY ) sock_delay = atoi( value );
  124.     else is_it( ETHIP )  ethip( value );
  125.     else is_it( MSS ) _mss = atoi( value );
  126.     else is_it( BOOTP ) _bootphost = resolve( value );
  127.     else is_it( BOOTPTO) _bootptimeout = atoi( value );
  128.     else is_it( DOMTO ) _domaintimeout = atoi( value );
  129.     else is_it( INACTIVE ) sock_inactive = atoi( value );
  130.     else is_it( MULTIHOMES ) multihomes = atoi( value );
  131.     else is_it( PRINT ) {
  132.     outs( value );
  133.     outs( "\n" );
  134.     }
  135.     else is_it( INCLUDE ) {
  136.         if ( *(p = value) == '?') p++;
  137.         if ((i = _open( p, O_RDONLY | O_TEXT )) != 0xffff ) {
  138.             _close( i );
  139.             tcp_config( p );
  140.         } else if ( *value != '?' ) {
  141.         outs("\nUnable to open '");
  142.         outs( p );
  143.         outs("'\n");
  144.         }
  145.     }
  146.     else {
  147.     if ( usr_init )
  148.         (*usr_init)(name, value);
  149.     }
  150. }
  151.  
  152. char *watfname = "DOSLYNX.CFG";
  153.  
  154. tcp_config( char *path )
  155. {
  156.     char name[80], *temp;
  157.     char value[80], ch[2];
  158.     int  quotemode;
  159.     int f, mode;
  160.  
  161.     if (!path) {
  162. #ifdef WWW
  163.     extern char *cp_inipath;
  164.     if ((path = cp_inipath) != NULL ) {
  165. #else
  166.     if ((path = getenv( watfname )) != NULL ) {
  167. #endif /* WWW */
  168.         path = strcpy( name, path );
  169.         strcat( name, "\\");
  170.     } else {
  171.         strcpy( name, _argv[0] );
  172.         path = ( *name && (name[1] == ':')) ? &name[2] : name;
  173.         if (!(temp = strrchr( path, '\\' ))) temp = path;
  174.         *(++temp) = 0;
  175.     }
  176. /*
  177.     strcpy( name, path );
  178.     strcat( name, "\\");
  179. */
  180.     strcat(name, watfname);
  181.     } else
  182.     strcpy( name, path );
  183.  
  184.     if ( ( f = _open( name, O_RDONLY | O_TEXT )) == -1 ) {
  185.     /* try local subdirectory */
  186.     if (( f = _open( watfname, O_RDONLY | O_TEXT )) == -1 ){
  187.     outs( watfname );
  188.     outs(" not found\n");
  189.         return( -1 );
  190.     }
  191.     }
  192.     *name = *value = mode = ch[1] = quotemode = 0;
  193.     while ( _read( f, &ch, 1 ) == 1) {
  194.     switch( *ch ) {
  195.         case  '\"': quotemode ^= 1;
  196.             break;
  197.         case  ' ' :
  198.         case  '\t': if (quotemode) goto addit;
  199.             break;
  200.  
  201.         case  '=' : if (quotemode) goto addit;
  202.             if (!mode) mode = 1;
  203.             break;
  204.         case  '#' :
  205.         case  ';' : if (quotemode) goto addit;
  206.             mode = 2;
  207.             break;
  208.         case  '\n':
  209.         case  '\r': if (*name && *value)
  210.                 set_values(name, value);
  211.             *name = *value = quotemode = mode = 0;
  212.             break;
  213.         default   :
  214. addit:
  215.                   switch (mode ) {
  216.             case 0 : strcat(name, ch);
  217.                  break;
  218.             case 1 : strcat(value, ch);
  219.                  break;
  220.             }
  221.             break;
  222.     }
  223.     }
  224.     _close(f );
  225.     return( 0 );
  226. }
  227.  
  228.